package com.mf.web;

import com.mf.common.sso.LoginContext;
import com.mf.domain.general.tree.MenuTree;
import com.mf.service.authority.ResourceInfoService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 默认控制器
 *
 * User: zhaoming
 * DateTime: 12-10-24 下午4:10
 * Version: 1.0
 */
@Controller
@RequestMapping("/")
public class DefaultController {

    public Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ResourceInfoService resourceInfoService;
    /**
     * 登陆页面
     * @param request 页面请求对象
     * @param response 页面响应对象
     */
    @RequestMapping(value = "/login", method = {RequestMethod.POST,RequestMethod.GET})
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(passWord)){
            logger.info("登陆失败:用户名或密码不允许为空");
            return "/login";
        }

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName,passWord);
        try {
            subject.login(token);
            return "/index";
        }catch (Exception e) {
            logger.error("登陆失败", e);
            token.clear();
            return "/login";
        }
    }

    /**
     * 系统初始化页面
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    /**
     * 获取资源菜单
     * @param request 页面请求对象
     * @param response 页面响应对象
     */
    @RequestMapping(value = "/menu", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<MenuTree> menu(HttpServletRequest request, HttpServletResponse response) {
        return resourceInfoService.selectMenuResource();
    }

    /**
     * 登出页面
     * @param request 页面请求对象
     * @param response 页面响应对象
     */
    @RequestMapping(value = "/logout", method = {RequestMethod.POST,RequestMethod.GET})
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            // session 会销毁，在SessionListener监听session销毁，清理权限缓存
            logger.info("用户" + subject.getSession().getAttribute("userCode") + "退出登录");
            subject.logout();
            LoginContext.removeLoginUser();
        }
        return "/login";
    }
}
